<template>
  <el-input v-model="dataVal" clearable v-on:input="oninput"/>
</template>
<script lang="ts" setup>
import {ref, watch} from "vue";
import {replace} from "lodash";

const props = defineProps({
  zsInputType: String,
  modelValue: {
    type: String
  },
})
const emit = defineEmits(['update:modelValue'])
const dataVal: any = ref("")
//输入值效验函数
const oninput = (val: any) => {
  if (props.zsInputType == "phone") {
    //输入值首先匹配正则
    dataVal.value = val.replace(/[^\d]/g, '')
    //输入值其次进行字符串长度截取
    dataVal.value = strValue(dataVal.value)
    emit("update:modelValue", dataVal.value)
  } else if (props.zsInputType == "sort") {
    dataVal.value = val.replace(/[^0-9]/g, '')
    emit("update:modelValue", dataVal.value)
  }
}

const strValue = (val: any) => {
  if (val.length > 1) {
    let arr = Object.values(val);
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] === "1") {
        let aaa = val.substring(i, arr.length);
        let bbb = twoStrValue(aaa);
        return bbb
      } else {
        strValue(val.substring(i + 1, arr.length))
      }
    }
  } else {
    if (val === "1") {
      return val
    } else {
      return ""
    }
  }
}

const twoStrValue = (val: any) => {
  debugger
  if (val.length > 1) {
    let arr2: any = Object.values(val);
    for (let i = 0; i < arr2.length; i++) {
      if ("3456789".includes(arr2[1])) {
        if (val.length>11){
          return val.substring(0,11)
        }
        return val
      } else {
        return val.substring(0, 1)
      }
    }
  } else {
    return val;
  }
}
</script>

